var send_flag = true;
$(function(){
    var reg_flag = true,reg_count = 0;
    $.mvalidateExtend({
        //验证手机号 密码  校验码
        mobile:{
            required : true,   
            pattern : /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-3,5-9]{1})|(17[0,1,3,5-8]{1})|(18[0-9]{1}))+\d{8})$/,
            each:function(){

            },
            descriptions:{
                required : '<div class="field-invalidmsg">请填写手机号</div>',
                pattern : '<div class="field-invalidmsg">请填写您的真实手机号码</div>',
            }
        },
        code:{
            required : true,   
            pattern : /\b\d{6}\b/,
            each:function(){

            },
            descriptions:{
                required : '<div class="field-invalidmsg">请填写手机验证码</div>',
                pattern : '<div class="field-invalidmsg">请填写有效的验证码</div>',
            }
        },
        pwd:{
            required : true,   
            pattern : /^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z]{1}[0-9A-Za-z]{5,16}$/,
            each:function(){

            },
            descriptions:{
                required : '<div class="field-invalidmsg">请输入密码</div>',
                pattern : '<div class="field-invalidmsg">6~16 位密码，必须由数字和字母组成，区分大小写</div>',
            }
        }
    });
     $("#form-reg").mvalidate({
        type:1,
        onKeyup:true,
        sendForm:true,
        firstInvalidFocus:false,
        descriptions: {
        },
        valid:function(event,options){
            add_reg_oper_listener();
            var url = $('#form-reg').attr("data-href");
            reg_flag = false;
            $("#form-reg-btn").val("注册中...");
            $.ajax({
                type: "POST",
                url: url,
                async:true, 
                data: 'uCode=' + $('#uCode').val()+ '&uMobile=' + $('#uMobile').val()+ '&uPass=' + $('#uPass').val()+ '&redirectUrl=' + $('#redirectUrl').val(),
                success: function(resp) {
                    resp = $.parseJSON(resp);
                    if(resp.reqStatus){
                        window.location.href = resp.url;
                    }else{
                        cancel_reg_oper_listener();
                        $("#form-reg-btn").val("重新注册");
                        $.mvalidateTip(resp.errorMsg);
                        return false;
                    }
                }
            });
        },
        invalid:function(event, status, options){
            //点击提交按钮时,表单未通过验证触发函数
        },
        eachField:function(event,status,options){
            //点击提交按钮时,表单每个输入域触发这个函数 this 执向当前表单输入域，是jquery对象
        },
        eachValidField:function(val){},
        eachInvalidField:function(event, status, options){},
    })
    
    function add_reg_oper_listener(){
        if(reg_count == 2){
            reg_count = 0;
            $.mvalidateTip("操作过于频繁");return false;
        }
        if(!reg_flag){
            reg_count ++;
            $.mvalidateTip("正在处理注册操作");return false;
        }
    };
    function cancel_reg_oper_listener(){
        reg_count = 0;
        reg_flag = true;
    }
    
    var total_time = 60;
    var isMobile = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-3,5-9]{1})|(17[0,1,3,5-8]{1})|(18[0-9]{1}))+\d{8})$/;
    
    $("#send-code-btn").on("click",function(){
        var _self = $(this);
        if(!send_flag){
            $.mvalidateTip("操作过于频繁");return;
        }
        
        var start_time = function(){
            for (var i = 1; i <= total_time; i++) {
                window.setTimeout("update_p("+ i + "," + total_time + ")", i * 1000);
            }
        }
        var send = function(){
            var mobile = $("#uMobile").val();
            if(mobile == ""){
                $.mvalidateTip("请输入手机号");return;
            }
            if(!isMobile.test(mobile)){
                $.mvalidateTip("手机格式不正确");return;
            }
            send_flag = false;
            $.ajax({
                type: "POST",
                url: "/ajax/send_reg_code",
                async:true, 
                data: 'uMobile=' + mobile,
                success: function(resp) {
                    resp = $.parseJSON(resp);
                    if(resp.reqStatus){
                        $.mvalidateTip("验证码已发送");
                        return start_time();
                    }else{
                        send_flag = true;
                        $.mvalidateTip(resp.errorMsg);
                        return;
                    }
                }
            });
        }
        send();
    });
});
var update_p = function(num,t){
    if (num == t) {
        $("#send-code-btn").text("重新获取").addClass("col-red").removeClass("col-grey999");
        send_flag = true;
    } else {
        var printnr = t - num;
        $("#send-code-btn").text("剩余"+printnr + "秒").removeClass("col-red").addClass("col-grey999");
    }
}